
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TrainingsApp.Model;
using TrainingsApp.Helper;
using WisDragon.Framework.Data.DataAccess;

namespace TrainingsApp.DAL {
    public class TS_User_Children_DAL {
        public static TS_User_Children_Collecion TS_User_Children_ListByPaging(int pageSize, int pageIndex) {
            TS_User_Children_Collecion list = new TS_User_Children_Collecion();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Children_ListByPaging");
                cmd.SetParameterValue("@pageSize", pageSize);
                cmd.SetParameterValue("@pageIndex", pageIndex);
                List<TS_User_Children_Entity> pageList = cmd.ExecuteEntityList<TS_User_Children_Entity>();
                list.TS_User_Children_List = pageList;
                list.TotalCount = Convert.ToInt32(cmd.GetParameterValue("@recordCount"));
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return list;

        }


        public static TS_User_Children_Collecion TS_User_Children_ListToGridByPaging(int pageSize, int pageIndex, string where) {
            TS_User_Children_Collecion list = new TS_User_Children_Collecion();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Children_ListToGridByPaging");
                cmd.SetParameterValue("@pageSize", pageSize);
                cmd.SetParameterValue("@pageIndex", pageIndex);
                cmd.SetParameterValue("@where", where);
                List<TS_User_Children_Entity> pageList = cmd.ExecuteEntityList<TS_User_Children_Entity>();
                list.TS_User_Children_List = pageList;
                list.TotalCount = Convert.ToInt32(cmd.GetParameterValue("@recordCount"));
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return list;

        }

        public static TS_User_Children_Entity TS_User_Children_View(System.Int64 UserId) {
            TS_User_Children_Entity c = default(TS_User_Children_Entity);
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Children_View");
                cmd.SetParameterValue("@UserId", UserId);
                c = cmd.ExecuteEntity<TS_User_Children_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
            return c;
        }

        public static TS_User_Children_Entity TS_User_Children_Add(TS_User_Children_Entity entity, TS_User_Patriarch_Entity parentEntity) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Children_Add");

                cmd.SetParameterValue("@UserName", entity.UserName);
                cmd.SetParameterValue("@Password", entity.Password);
                cmd.SetParameterValue("@Name", entity.Name);
                cmd.SetParameterValue("@Gender", entity.Gender);
                cmd.SetParameterValue("@BirthDay", entity.BirthDay);
                cmd.SetParameterValue("@IdentityCard", entity.IdentityCard);
                cmd.SetParameterValue("@Mobile", entity.Mobile);
                cmd.SetParameterValue("@Email", entity.Email);
                cmd.SetParameterValue("@PhotoImage", entity.PhotoImage);
                cmd.SetParameterValue("@UserType", entity.UserType);
                cmd.SetParameterValue("@UserId", entity.UserId);

                cmd.SetParameterValue("@UserSource", entity.UserSource);
                cmd.SetParameterValue("@ParentUserName", parentEntity.UserName);
                cmd.SetParameterValue("@ParentPassword", parentEntity.Password);
                cmd.SetParameterValue("@ParentName", parentEntity.Name);
                cmd.SetParameterValue("@ParentGender", parentEntity.Gender);
                cmd.SetParameterValue("@ParentBirthDay", parentEntity.BirthDay);
                cmd.SetParameterValue("@ParentIdentityCard", parentEntity.IdentityCard);
                cmd.SetParameterValue("@ParentMobile", parentEntity.Mobile);
                cmd.SetParameterValue("@ParentEmail", parentEntity.Email);
                cmd.SetParameterValue("@ParentPhotoImage", parentEntity.PhotoImage);
                cmd.SetParameterValue("@ParentUserType", parentEntity.UserType);
                cmd.SetParameterValue("@ParentUserId", parentEntity.UserId);

                cmd.SetParameterValue("@WorkOfCompany", parentEntity.WorkOfCompany);
                cmd.SetParameterValue("@Position", parentEntity.Position);
                cmd.SetParameterValue("@Refrence", parentEntity.Refrence);


                cmd.SetParameterValue("@StuCode", entity.StuCode);
                cmd.SetParameterValue("@ClassesId", entity.ClassesId);
                cmd.SetParameterValue("@SchoolId", entity.SchoolId);
                
                cmd.ExecuteNonQuery();

                long userId = Convert.ToInt64(cmd.GetParameterValue("UserId"));
                long parentUserId = Convert.ToInt64(cmd.GetParameterValue("ParentUserId"));

                entity.UserId = userId;
                entity.Patriarch = parentUserId;
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return entity;
        }

        public static void TS_User_Children_Update(TS_User_Children_Entity entity, TS_User_Patriarch_Entity parentEntity) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Children_Update");


                cmd.SetParameterValue("@Password", entity.Password);
                cmd.SetParameterValue("@Name", entity.Name);
                cmd.SetParameterValue("@Gender", entity.Gender);
                cmd.SetParameterValue("@BirthDay", entity.BirthDay);
                cmd.SetParameterValue("@IdentityCard", entity.IdentityCard);
                cmd.SetParameterValue("@Mobile", entity.Mobile);
                cmd.SetParameterValue("@Email", entity.Email);
                cmd.SetParameterValue("@PhotoImage", entity.PhotoImage);
                cmd.SetParameterValue("@UserType", entity.UserType);
                cmd.SetParameterValue("@UserId", entity.UserId);
                cmd.SetParameterValue("@ParentPassword", parentEntity.Password);
                cmd.SetParameterValue("@ParentName", parentEntity.Name);
                cmd.SetParameterValue("@ParentGender", parentEntity.Gender);
                cmd.SetParameterValue("@ParentBirthDay", parentEntity.BirthDay);
                cmd.SetParameterValue("@ParentIdentityCard", parentEntity.IdentityCard);
                cmd.SetParameterValue("@ParentMobile", parentEntity.Mobile);
                cmd.SetParameterValue("@ParentEmail", parentEntity.Email);
                cmd.SetParameterValue("@ParentPhotoImage", parentEntity.PhotoImage);
                cmd.SetParameterValue("@ParentUserType", parentEntity.UserType);
                cmd.SetParameterValue("@ParentUserId", parentEntity.UserId);

                cmd.SetParameterValue("@WorkOfCompany", parentEntity.WorkOfCompany);
                cmd.SetParameterValue("@Position", parentEntity.Position);
                cmd.SetParameterValue("@Refrence", parentEntity.Refrence);
                cmd.SetParameterValue("@StuCode", entity.StuCode);
                cmd.SetParameterValue("@ClassId", entity.ClassesId);
                cmd.SetParameterValue("@SchoolId", entity.SchoolId);
                
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
        }

        public static void TS_User_Children_Delete(System.Int64 UserId) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Children_Delete");
                cmd.SetParameterValue("@UserId", UserId);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
        }
    }
}
